#include "json_class.h"

json_class::json_class()
{
    //打开json文件
    QFile loadFile(":/new/prefix1/node.json");
    if(!loadFile.open(QIODevice::ReadOnly))
    {
        qDebug() << "could't open projects json";
        return ;
    }

    QByteArray allData = loadFile.readAll();
    loadFile.close();
    //JSON解析中报告的错误
    QJsonParseError jsonError;
    //使用QJsonDocument来读写json文档
    //fromJson将JSON 解析成utf-8文档
    QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &jsonError));

    if(jsonError.error != QJsonParseError::NoError)
    {
        qDebug() << "json error!" << jsonError.errorString();
        return;
    }
    //获取文档中包含object对象
    QJsonObject rootObj = jsonDoc.object();
    //存放字符串列表
    //返回此对象中所有键的列表,列表按字母顺序排列。
    QStringList keys = rootObj.keys();
    for(int i = 0; i < keys.size(); i++)
    {
        qDebug() << "key" << i << " is:" << keys.at(i);
    }

    //因为是预先定义好的JSON数据格式，所以这里可以这样读取
    if(rootObj.contains("version"))
    {
        qDebug() << "version is:" << rootObj["version"].toString();
    }

    if(rootObj.contains("mqtt_server"))
    {
        QJsonObject subObj_mqtt_server = rootObj.value("mqtt_server").toObject();
        qDebug() << "addr is:" << subObj_mqtt_server.value("addr").toString();
        qDebug() << "port is:" << subObj_mqtt_server.value("port").toInt();
    }

    if(rootObj.contains("report"))
    {
        QJsonObject subObj_report = rootObj.value("report").toObject();
        qDebug() << "type is:" << subObj_report.value("type").toInt();
        qDebug() << "period is:" << subObj_report.value("period").toInt();
    }
    if(rootObj.contains("m0"))
    {
        QJsonObject subObj_m0 = rootObj.value("m0").toObject();
        qDebug() << "name is:" << subObj_m0.value("name").toString();

        QJsonArray subArray_m0data = subObj_m0.value("data").toArray();
        for(int i = 0; i< subArray_m0data.size(); i++)
        {
            QJsonObject thirdArray_data = subArray_m0data.at(i).toObject();
            qDebug() <<" key is:" << thirdArray_data.value("key").toInt();
            qDebug() <<" name is:" << thirdArray_data.value("name").toString();
            qDebug() <<" type is:" << thirdArray_data.value("type").toInt();
        }
    }
    if(rootObj.contains("modbus"))
    {
        QJsonObject subObj_modbus = rootObj.value("modbus").toObject();
        qDebug() << "name is:" << subObj_modbus.value("name").toString();

        QJsonArray subArray_modbusdata = subObj_modbus.value("data").toArray();
        for(int i = 0; i< subArray_modbusdata.size(); i++)
        {
            QJsonObject thirdArray_data = subArray_modbusdata.at(i).toObject();
            qDebug() <<" key is:" << thirdArray_data.value("key").toInt();
            qDebug() <<" name is:" << thirdArray_data.value("name").toString();
            qDebug() <<" addr is:" << thirdArray_data.value("addrr").toInt();
            qDebug() <<" type is:" << thirdArray_data.value("type").toInt();
        }
    }

}
